package net.expectx.pay.merchant.tag;


import net.expectx.pay.dao.bean.SystemSetting;
import net.expectx.pay.dao.bean.SystemSettingExample;
import net.expectx.pay.merchant.common.AdminConstants;
import net.expectx.pay.rpc.api.SystemSettingService;
import org.springframework.web.servlet.tags.RequestContextAwareTag;

import javax.servlet.jsp.JspException;
import javax.servlet.jsp.JspWriter;
import java.util.List;

/**
 * @author lijian
 * 系统设置自定义标签
 */
public class SettingTag extends RequestContextAwareTag {
    private String key;
    private String defaultVal;



    @Override
    public int doStartTagInternal() throws JspException {
        JspWriter out=this.pageContext.getOut();
        try {
            SystemSettingService systemSettingService = (SystemSettingService) this.getRequestContext().getWebApplicationContext().getBean(SystemSettingService.class);
            /*Subject subject = SecurityUtils.getSubject();
            Session session = subject.getSession();*/
            List<SystemSetting> systemSettings=null;//(List<SystemSetting>) session.getAttribute("systemSettings");
            if (null==systemSettings){
                SystemSettingExample systemSettingExample = new SystemSettingExample();
                SystemSettingExample.Criteria criteria = systemSettingExample.createCriteria();
                criteria.andIsDeletedEqualTo(AdminConstants.NO);
                systemSettings=systemSettingService.selectByExample(systemSettingExample);
                //session.setAttribute("systemSettings",systemSettings);
            }
            String attrValue=null;
            for (SystemSetting systemSetting:systemSettings){
                if (key.equals(systemSetting.getAttrName())){
                    attrValue=systemSetting.getAttrValue();
                }
            }
            if (attrValue!=null){
                out.println(attrValue);
            }else{
                out.println(defaultVal);
            }

        } catch (Exception e) {
            e.printStackTrace();
        }
        return super.doEndTag();
    }

    public String getKey() {
        return key;
    }

    public void setKey(String key) {
        this.key = key;
    }

    public String getDefaultVal() {
        return defaultVal;
    }

    public void setDefaultVal(String defaultVal) {
        this.defaultVal = defaultVal;
    }
}
